package com.luzhikun.cookieTest;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Rookie Coder
 * @Description 请求获取Cookie
 * @Date 2021/5/1  18:25
 **/
public class RequestCookie extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        // 请求获取到响应传过来的Cookie参数
        // getCookies():返回响应对象中所有的Cookie，返回值是一个Cookie[]

        Cookie[] cookies = req.getCookies();
        PrintWriter writer = resp.getWriter();

        // 遍历获取
        for (Cookie cookie : cookies) {
            if ("address".equals(cookie.getName())) {
                writer.println("恭喜你点餐成功===" + cookie.getValue() + "将在5分钟后做好");
            }
        }

//        String requestedSessionId = req.getRequestedSessionId();
//        System.out.println(requestedSessionId);
//        HttpSession session = req.getSession(false);
//        System.out.println(session);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
